import os
import yaml
import configargparse
from utils.progress_bar import bar_show, false_bar


# NOTE: you need this func to generate our sphinx doc
def get_parser():
    """Get Parser."""
    parser = configargparse.ArgumentParser(
        description = 'Create a new regular cropus',
        config_file_parser_class = configargparse.YAMLConfigFileParser,
        # formatter_class=configargparse.ArgumentDefaultsHelpFormatter
    )
    # general configuration
    base_dir = os.path.dirname(__file__)
    # parser.add('--config', is_config_file=True,help='config file path')
    parser.add_argument('--config', default='config/test.yaml', required=True, type=str, help='config file path')
    parser.add_argument('--base', default=base_dir, required=False, type=str, help='Base dir for project')
    parser.add_argument('--file_path', default='data/files/crops.txt', required=False, type=str, help='File path will be processed')
    parser.add_argument('--file_dir', default='data/files', required=False, type=str, help='Dir path will be processed')
    parser.add_argument('--save_path', default='data/save/result.txt', required=False, type=str, help='Save path')
    parser.add_argument('--split_dir', default='data/split', required=False, type=str, help='Split path')
    # paragraph length limit
    parser.add_argument('--para_limit', default=[2, 25], required=False, type=list, help="loc 1:the min length, loca 2: the max length")
    # load json content
    parser.add_argument('--json_key_word',default='', required=False, type=str, help='When load json data, you can extract these data by json keyWord' )
    # progress bar for tools
    parser.add_argument('--bar_show', default=bar_show, required=False, help='Class bar')
    parser.add_argument('--false_bar', default=false_bar, required=False, help='Class false bar')
    # train, valid, test split
    parser.add_argument('--split', default=[7, 2, 1], required=False, help='loc1: train datasets, loc2: valid datasets, loc3: test datasets')
    

    args = parser.parse_args()
    print(parser.format_values())   # Show default parser.
    print(parser.format_help()) # Show helpers
    return args


# load config.yaml file
def load_config(config_file):
    loder = {}
    try:
        fr = open(config_file, 'r', encoding='utf-8')
    except Exception as e:
        print('error:', e)
    else:
        loder = yaml.safe_load(fr.read())
    
    return loder